<template>
  <div id="app">
    <div class="left">
      <div
        @dragstart="(e) => dragstart(e, item)"
        @dragend="dragend"
        draggable
        class="comp-item"
        :key="index"
        v-for="(item, index) in comps"
      >
        {{ item.name }}
      </div>
    </div>
    <div class="right">
      <CnhisDrag v-model="values" ref="cnhisDrag">
        <component 
          :is="item.type"
          :data="item"
          :key="item.id"
           v-for="item in values"
        />
        <!-- <div
          class="block-item"
          :data="item"
          :key="item.id"
          v-for="item in values"
        >
          {{ item.name }}
        </div> -->
      </CnhisDrag>
    </div>
  </div>
</template>

<script>
import CnhisDrag from "./components/CnhisDrag.vue";
import { H5drag } from "./components/cnhisDragutils";
import A from "./components/comps/A";
import B from "./components/comps/B";

export default {
  name: "App",
  data() {
    return {
      comps: [
        {
          type: "1",
          name: "组件1",
        },
        {
          type: "2",
          name: "组件2",
        },
        {
          type: "3",
          name: "组件3",
        },
      ],
      values: [
        {
          top: 30,
          left: 200,
          id: 1,
          name: "组件2222",
          type: 'A'
        },
        {
          top: 300,
          left: 400,
          id: 2,
          name: "组件333",
          type: 'B'
        },
      ],
      dragstart: () => {},
      dragend: () => {},
    };
  },
  mounted() {
    const { dragstart, dragend } = H5drag(this.$refs.cnhisDrag, {
      dropCb: (e, comp) => {
        const block = {
          ...comp,
          top: e.offsetY - 40,
          left: e.offsetX - 100,
          zIndex: 1,
          type: 'A',
          id: new Date().getTime(),
          props: {},
          model: {},
        };
        this.values.push(block);
      },
    });
    this.dragstart = dragstart;
    this.dragend = dragend;
  },
  methods: {
  },
  components: {
    CnhisDrag,
    A,
    B
  },
};
</script>

<style>
html,
body {
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
}
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  display: flex;
  width: 100%;
  height: 100%;
}
.left {
  width: 300px;
  height: 100%;
  border-right: 1px solid #2c3e50;
  display: flex;
  align-items: center;
  flex-direction: column;
  padding: 20px;
}
.right {
  flex: 1;
  height: 100%;
}
.comp-item {
  width: 180px;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #2c3e50;
  margin-bottom: 30px;
}
.block-item {
  width: 200px;
  height: 80px;
  background: yellow;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
